<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\AppException;
use App\Models\User;
use App\Services\AppUser;
use App\Http\Requests\AdminPost;
use App\Services\ValidatorService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Monolog\Logger;
use Services\HandleDataService;
use Tymon\JWTAuth\JWTAuth;
use App\Services\ExcelExportService;
use Psr\Log\LoggerInterface;

/**
 * Class ExcelController
 * @package App\Http\Controllers
 */
class ExcelController extends BaseController
{
    public $serviceName = ExcelExportService::class;

    private $logger;

    public function __construct(LoggerInterface $logger = null)
    {
        $this->logger = $logger;
    }

    public function export()
    {
        if ($this->logger) {
            $this->logger->info('Doing work');
        }
        $this->logger->error('Oh no!', array('exception' => 111));

        try {
            $directory = storage_path('excel') . '/export';
            $fileName = rand(1000, 9999) . time() . ".xlsx";//导出文件名
            $header = ['user_id' => '销售名称', 'name' => '客户名称'
            ];
            $widthArray = ['A' => 30, 'B' => 30, 'C' => 30, 'D' => 50, 'E' => 50];
            $dataBuild = User::query();
            $service = new ExcelExportService();
            $service->exportDataBySheet($directory, $fileName, $dataBuild, $header, '', 1000, $widthArray);
            return response()->download($directory . '/' . $fileName, '导出文件.xlsx', $headers = ['Content-Type' => 'application/zip;charset=utf-8']);
        } catch (\ Exception $exception) {
            echo $exception->getMessage();
            die;
            throw new AppException('export_fail');

        }
        return $this->success(['$user']);
    }

    public function import()
    {

        $directory = storage_path('excel') . '/export';
        $fileName =  "97761607078208.xlsx";//导出文件名
        # 创建读操作
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
        # 打开文件、载入excel表格
        $spreadsheet = $reader->load($directory . '/' . $fileName);
        # 获取活动工作薄
        $sheet = $spreadsheet->getActiveSheet();

        # 获取总列数
        $highestColumn = $sheet->getHighestColumn();
        # 获取总行数
        $highestRow = $sheet->getHighestRow();
        # 列数 改为数字显示
        $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
        $log = [];


        for($a=2;$a<=2;$a++){
            $title = $sheet->getCellByColumnAndRow(1,$a)->getValue();
            $cat_fname = $sheet->getCellByColumnAndRow(2,$a)->getValue();
            $cat_name = $sheet->getCellByColumnAndRow(3,$a)->getValue();
            $price = $sheet->getCellByColumnAndRow(4,$a)->getValue();
            $img = $sheet->getCellByColumnAndRow(5,$a)->getValue();

//            $cat_fid = find('shop_cat','id','name="'.$cat_fname.'"');
//            $cat_id = find('shop_cat','id','name="'.$cat_name.'"');
//            $data = [
//                'title' => $title,
//                'cat_fid' => $cat_fid['id'],
//                'cat_id' => $cat_id['id'],
//                'price' => $price,
//                'img' => $img,
//                'add_time' => time(),
//            ];
//            $ins = insert('shop_list',$data);
//            if($ins){
//                $log[] = '第'.$a.'条，插入成功';
//            }else{
//                $log[] = '第'.$a.'条，插入失败';
//            }
        }
//        echo json_encode(['code'=>0,'msg'=>'成功','data'=>$log]);
    }

}
